/**
 * Copyright (C), 2020, JieYi Software System Co., Ltd.
 * All rights reserved.
 * FileName:       MdmsSshserverController.java
 *
 * @Description:
 * @author: feiwe
 * Modification History:
 * Date         Author        Version        Discription
 * -------------------------------------------------------
 * 2020/12/3      feiwe                         add
 */


package com.fengwei.controller;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fengwei.form.MapForObject.MapForObject;
import com.fengwei.form.MdmsUser;
import com.fengwei.requestdata.sshserver.MdmsSshserverAddData;
import com.fengwei.requestdata.sshserver.MdmsSshserverAddServerUserData;
import com.fengwei.requestdata.sshserver.MdmsSshserverDelData;
import com.fengwei.requestdata.sshserver.MdmsSshserverDelServerUserData;
import com.fengwei.requestdata.sshserver.MdmsSshserverEditData;
import com.fengwei.requestdata.sshserver.MdmsSshserverGetServerUsersByServerIdData;
import com.fengwei.requestdata.sshserver.MdmsSshserverPagingListData;
import com.fengwei.requestdata.sshserver.MdmsSshserverQueryMyServersData;
import com.fengwei.requestdata.user.MdmsUserDelSignUserData;
import com.fengwei.requestdata.user.MdmsUserGetUsersForSshServerSignedData;
import com.fengwei.requestdata.user.MdmsUserSignUserData;
import com.fengwei.service.ConsoleCommonService;
import com.fengwei.service.MdmsSshserverService;
import com.fengwei.service.MdmsUserService;
import com.fengwei.util.PltResult;
import com.fengwei.util.ResponseUtil;
import com.fengwei.verify.Verify;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
public class MdmsSshserverController {
    private final static Logger logger = LoggerFactory.getLogger(MdmsSshserverController.class);

    @Autowired
    private ConsoleCommonService consoleCommonService;

    @Autowired
    private MdmsSshserverService mdmsSshserverService;

    @Autowired
    private MdmsUserService mdmsUserService;

    @GetMapping(value = "/mdmsSshserver/loadPage")
    public String loadPage(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "mdmsSshserver";
    }

    /**
     * 分页查询
     */
    @PostMapping(path = "/mdmsSshserver/pagingList")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> pagingList(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsSshserverPagingListData mdmsSshserverPagingListData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverPagingListData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.pagingList(mdmsSshserverPagingListData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 添加
     */
    @PostMapping(path = "/mdmsSshserver/add")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> add(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsSshserverAddData mdmsSshserverAddData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverAddData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.add(mdmsSshserverAddData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 编辑
     */
    @PostMapping(path = "/mdmsSshserver/edit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> edit(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsSshserverEditData mdmsSshserverEditData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverEditData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.edit(mdmsSshserverEditData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 删除
     */
    @PostMapping(path = "/mdmsSshserver/del")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> del(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        consoleCommonService.checkAndGetUser(request);
        MdmsSshserverDelData mdmsSshserverDelData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverDelData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.del(mdmsSshserverDelData);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 根据serverId查询所有的服务器用户
     */
    @PostMapping(path = "/mdmsSshserver/getServerUsersByServerId")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> getServerUsersByServerId(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsSshserverGetServerUsersByServerIdData mdmsSshserverGetServerUsersByServerIdData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverGetServerUsersByServerIdData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.getServerUsersByServerId(mdmsSshserverGetServerUsersByServerIdData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 添加服务器用户
     */
    @PostMapping(path = "/mdmsSshserver/addServerUser")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> addServerUser(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsSshserverAddServerUserData mdmsSshserverAddServerUserData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverAddServerUserData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.addServerUser(mdmsSshserverAddServerUserData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 删除服务器用户
     */
    @PostMapping(path = "/mdmsSshserver/delServerUser")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> delServerUser(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsSshserverDelServerUserData mdmsSshserverDelServerUserData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverDelServerUserData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.delServerUser(mdmsSshserverDelServerUserData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 获取当前的授权的用户
     */
    @PostMapping(path = "/mdmsSshserver/getUsersForSshServerSigned")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> getUsersForSshServerSigned(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserGetUsersForSshServerSignedData mdmsUserGetUsersForSshServerSignedData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsUserGetUsersForSshServerSignedData);
        // 正式处理业务
        Map<String, Object> data = mdmsUserService.getUsersForSshServerSigned(mdmsUserGetUsersForSshServerSignedData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 授权
     */
    @PostMapping(path = "/mdmsSshserver/signUser")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> signUser(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserSignUserData mdmsUserSignUserData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsUserSignUserData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.signUser(mdmsUserSignUserData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 授权
     */
    @PostMapping(path = "/mdmsSshserver/delSignUser")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Map<String, Object> delSignUser(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsUserDelSignUserData mdmsUserDelSignUserData =
                new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                        new TypeReference<>() {
                        });

        // 校验传入参数
        Verify.verifyData(mdmsUserDelSignUserData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.delSignUser(mdmsUserDelSignUserData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

    /**
     * 查询登录用户所拥有的所有服务器
     */
    @PostMapping(path = "/mdmsSshserver/queryMyServers")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN','DBA','USER')")
    public Map<String, Object> queryMyServers(@RequestBody Map<String, Object> jsonObject, HttpServletRequest request) throws Exception {
        MdmsUser mdmsUser = consoleCommonService.checkAndGetUser(request);
        MdmsSshserverQueryMyServersData mdmsSshserverQueryMyServersData = new ObjectMapper().convertValue(new MapForObject(jsonObject).getData(),
                new TypeReference<>() {
                });

        // 校验传入参数
        Verify.verifyData(mdmsSshserverQueryMyServersData);
        // 正式处理业务
        Map<String, Object> data = mdmsSshserverService.queryMyServers(mdmsSshserverQueryMyServersData, mdmsUser, request);

        return ResponseUtil.toJson(PltResult.RESULT_0000, data);
    }

}
